﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace ApiManage.Core.Utils
{
    public static class ObjectUtils
    {
        /// <summary>
        /// 获取一个类指定的属性值
        /// </summary>
        /// <param name="obj">object对象</param>
        /// <param name="field">属性名称</param>
        /// <returns></returns>
        public static object GetPropertyValue(this object obj, string field)
        {
            if (obj == null) return null;
            var t = obj.GetType();
            var property = from pi in t.GetProperties() where string.Equals(pi.Name, field, StringComparison.CurrentCultureIgnoreCase) select pi;
            var propertyInfos = property as PropertyInfo[] ?? property.ToArray();
            return propertyInfos.Any() ? propertyInfos.First().GetValue(obj, null) : null;
        }
    }
}
